<?php


namespace addon\hotel\model;

use app\model\BaseModel;
use app\model\system\Document;
use Exception;

class HotelSettlement extends BaseModel
{
    /**
     * 平台整体对店铺进行结算
     * @param int $end_time 截至时间
     */
    public function shopSettlement($end_time)
    {
        //结算周期初始化
        //开始记录时间

        $last_period = model('hotel_settlement_period')->getFirstData([], 'period_start_time, period_end_time', 'period_id desc');
        if (!empty($last_period)) {
            $start_time = $last_period['period_end_time'];
        } else {
            $start_time = 0;
        }
        if ($end_time - $start_time < 3600 * 6) {
            return $this->success();
        }

        $period_no = date('YmdHi') . rand(1000, 9999);
        $period_id = model("hotel_settlement_period")->add(["period_no" => $period_no, "create_time" => time(), "period_start_time" => $start_time, "period_end_time" => $end_time]);
        $hotel_num = 0;
        $order_money = 0;
        $hotel_money = 0;
        $platform_money = 0;
        $refund_money = 0;
        $refund_hotel_money = 0;
        $refund_platform_money = 0;
        $commission = 0;

        //店铺列表统计
        $hotel_list = model("hotel")->getList([['status', '=', 1]], 'hotel_id, hotel_name');
        //循环各个店铺数据
        foreach ($hotel_list as $k => $hotel) {

            $hotel_id = $hotel['hotel_id'];
            $settlement = model("hotel_order")->getInfo(
                [
                    ['order_status', '=', 10],
                    ['is_settlement', '=', 0],
                    ['hotel_id', '=', $hotel_id],
                    ['pay_type', '<>', 'OFFLINE_PAY'],
                    ['finish_time', '<=', $end_time]
                ]
                , 'sum(order_money) as order_money, sum(total_refund_money) as refund_money, sum(hotel_money) as hotel_money, sum(platform_money) as platform_money, sum(refund_hotel_money) as refund_hotel_money, sum(refund_platform_money) as refund_platform_money, sum(commission) as commission'
            );
            //dump($end_time);die;
            if (empty($settlement) || $settlement['order_money'] == null) {
                //注意总支出佣金要再订单完成后统计到订单
                $settlement = [
                    'order_money' => 0,
                    'refund_money' => 0,
                    'hotel_money' => 0,
                    'platform_money' => 0,
                    'refund_hotel_money' => 0,
                    'refund_platform_money' => 0,
                    'commission' => 0,
                ];

            }

            //根据退款 来计算平台优惠券 店铺货区部分

            $settlement['settlement_no'] = date('YmdHi') . $hotel_id . rand(1111, 9999);
            $settlement['hotel_id'] = $hotel_id;
            $settlement['hotel_name'] = $hotel['hotel_name'];
            $settlement['period_id'] = $period_id;
            $settlement['period_start_time'] = $start_time;
            $settlement['period_end_time'] = $end_time;

            $settlement_id = model("hotel_settlement")->add($settlement);
            model("hotel_order")->update(['is_settlement' => 1, 'settlement_time' => time(),"settlement_id" => $settlement_id], [['order_status', '=', 10], ['is_settlement', '=', 0], ['hotel_id', '=', $hotel['hotel_id']], ['finish_time', '<=', $end_time]]);
            $shop_account = new HotelAccount();
            //这里的备注还需要完善
            $shop_account->addAccount($hotel['hotel_id'], 'account', $settlement['hotel_money']  - $settlement['refund_hotel_money'] - $settlement['commission'], "order", $settlement_id, '结算，账单编号' . $settlement['settlement_no']);
            //平台也要进行统计
            $hotel_num = $hotel_num + 1;
            $order_money = $order_money + $settlement['order_money'];
            $hotel_money = $hotel_money + $settlement['hotel_money'];
            $platform_money = $platform_money + $settlement['platform_money'];
            $refund_money = $refund_money + $settlement['refund_money'];
            $refund_hotel_money = $refund_hotel_money + $settlement['refund_hotel_money'];
            $refund_platform_money = $refund_platform_money + $settlement['refund_platform_money'];
            $commission = $commission + $settlement['commission'];

        }
        $total_data = [
            'hotel_num' => $hotel_num,
            'order_money' => $order_money,
            'refund_money' => $refund_money,
            'hotel_money' => $hotel_money,
            'platform_money' => $platform_money,
            'refund_hotel_money' => $refund_hotel_money,
            'refund_platform_money' => $refund_platform_money,
            'commission' => $commission,
        ];
        model("hotel_settlement_period")->update($total_data, [['period_id', '=', $period_id]]);
        $account = new HotelAccount();
        $account->addAccount(0, 'account', $total_data['platform_money'] - $total_data['refund_platform_money'], "order", $period_id, '订单结算，账单编号:' . $period_no);
        return $this->success();
    }

    /**
     * 获取店铺结算周期结算信息
     * @param $condition
     * @param string $field
     * @return array
     */
    public function getShopSettlementInfo($condition, $field = '*')
    {
        $res = model('hotel_settlement')->getInfo($condition, $field);
        return $this->success($res);
    }

    /**
     * 获取店铺结算周期结算列表
     * @param array $condition
     * @param string $field
     * @param string $order
     * @param string $limit
     */
    public function getShopSettlementList($condition = [], $field = '*', $order = '', $limit = null)
    {

        $list = model('hotel_settlement')->getList($condition, $field, $order, '', '', '', $limit);
        return $this->success($list);
    }

    /**
     * 获取店铺结算周期结算分页列表
     * @param array $condition
     * @param number $page
     * @param string $page_size
     * @param string $order
     * @param string $field
     */
    public function getShopSettlementPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
    {

        $list = model('hotel_settlement')->pageList($condition, $field, $order, $page, $page_size);
        return $this->success($list);
    }

    /**
     * 获取店铺结算周期信息
     * @param unknown $condition
     * @param string $field
     */
    public function getShopSettlementPeriodInfo($condition, $field = '*')
    {
        $res = model('hotel_settlement_period')->getInfo($condition, $field);
        return $this->success($res);
    }

    /**
     * 获取店铺结算周期结算列表
     * @param array $condition
     * @param string $field
     * @param string $order
     * @param string $limit
     */
    public function getShopSettlementPeriodList($condition = [], $field = '*', $order = '', $limit = null)
    {

        $list = model('hotel_settlement_period')->getList($condition, $field, $order, '', '', '', $limit);
        return $this->success($list);
    }

    /**
     * 获取店铺结算周期结算分页列表
     * @param array $condition
     * @param number $page
     * @param string $page_size
     * @param string $order
     * @param string $field
     */
    public function getShopSettlementPeriodPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
    {

        $list = model('hotel_settlement_period')->pageList($condition, $field, $order, $page, $page_size);
        return $this->success($list);
    }

    /**
     * 获取店铺待结算订单金额
     */
    public function getWaitSettlementInfo($hotel_id)
    {
        $money_info = model("hotel_order")->getInfo([['hotel_id', '=', $hotel_id], ['order_status', '=', 10], ['is_settlement', '=', 0]], 'sum(order_money) as order_money, sum(refund_money) as refund_money, sum(hotel_money) as hotel_money, sum(platform_money) as platform_money, sum(refund_hotel_money) as refund_hotel_money, sum(refund_platform_money) as refund_platform_money, sum(commission) as commission');
        if (empty($money_info) || $money_info == null) {

            $money_info = [
                'order_money' => 0,
                'refund_money' => 0,
                'hotel_money' => 0,
                'platform_money' => 0,
                'refund_hotel_money' => 0,
                'refund_platform_money' => 0,
                'commission' => 0,
            ];

        }

        return $money_info;
    }

    /**
     * 获取店铺待结算订单金额
     */
    public function getShopSettlementData($condition = [])
    {
        $money_info = model("hotel_order")->getInfo($condition, 'sum(order_money) as order_money, sum(refund_money) as refund_money, sum(hotel_money) as hotel_money, sum(platform_money) as platform_money, sum(refund_hotel_money) as refund_hotel_money, sum(refund_platform_money) as refund_platform_money, sum(commission) as commission');
        if (empty($money_info) || $money_info == null) {

            $money_info = [
                'order_money' => 0,
                'refund_money' => 0,
                'hotel_money' => 0,
                'platform_money' => 0,
                'refund_hotel_money' => 0,
                'refund_platform_money' => 0,
                'commission' => 0,
            ];

        }

        return $money_info;
    }

}